<template>
  <div class="edit-password">
    <el-dialog
      :title="updateParams.type == 'edit' ? '修改课程' : '添加课程'"
      center
      :visible.sync="dialogFormVisible"
      @close="resetForm('ruleForm')"
      width="500px"
    >
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        size="small"
        label-width="80px"
        class="demo-ruleForm"
        style="width: 400px; margin: 0 auto;"
      >
        <el-form-item label="课程号" prop="course_id" ref="course_id">
          <el-input
            v-model.number="ruleForm.course_id"
            :disabled="updateParams.type === 'edit'"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="课程名" prop="course_name">
          <el-input
            v-model="ruleForm.course_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="学分" prop="course_credit">
          <el-input
            v-model.number="ruleForm.course_credit"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="课余量" prop="spare_time">
          <el-input v-model="ruleForm.spare_time" :disabled="updateParams.type === 'edit'" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { courseAdd, courseInfo, courseEdit } from '@/api/course'
export default {
  name: 'updateCourse-pop',
  data() {
    const validateCourseId = (rule, value, callback) => {
      if (value != '') {
        callback()
      } else {
        callback(new Error('请输入课程号'))
      }
    }
    return {
      dialogFormVisible: false,
      ruleForm: {
        course_id: '',
        course_name: '',
        course_credit: '',
        spare_time: '20'
      },
      rules: {
        course_id: [
          {
            validator: validateCourseId,
            trigger: 'blur'
          }
        ],
        course_name: [
          {
            required: true,
            message: '请输入课程名',
            trigger: 'blur'
          }
        ],
        course_credit: [
          {
            required: true,
            message: '请输入学分',
            trigger: 'blur'
          }
        ]
      },
      deptList: [],
      updateParams: {}
    }
  },

  methods: {
    // 1.打开弹窗
    openMask(record) {
      this.updateParams = record
      if (record.type === 'edit') {
        this.userDetail(record.course_id)
      }
      this.dialogFormVisible = true
    },

    // 2.根据用户id获取用户详情
    userDetail(course_id) {
      courseInfo({ course_id })
        .then(res => {
          if (res.result) {
            this.ruleForm = { ...res.data }
          } else {
            this.$message.error(res.message)
          }
        })
        .catch(err => {
          this.$message.error(err.message)
        })
    },

    // 4.注册/修改用户
    submitForm(formName) {
      if (this.updateParams.type == 'edit') {
        this.$refs['course_id'].clearValidate()
      }
      let api = this.updateParams.type === 'edit' ? courseEdit : courseAdd
      this.$refs[formName].validate(valid => {
        if (valid) {
          let params = {
            course_id: this.ruleForm.course_id,
            course_name: this.ruleForm.course_name,
            course_credit: Number(this.ruleForm.course_credit),
            spare_time: this.ruleForm.spare_time
          }
          api(params)
            .then(res => {
              if (res.result) {
                this.resetForm('ruleForm')
                this.$message.success(res.message)
                this.$emit('ok')
              } else {
                this.$message.error(res.message)
              }
            })
            .catch(err => {
              this.$message.error(err.message)
            })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="stylus" scoped>
.el-select {
  width 320px
}
</style>
